<?php
/**
 * Created by PhpStorm.
 * User: hengsu
 * Date: 2016/6/4
 * Time: 12:04
 */

namespace Home\Controller;
use Think\Controller;

class ComputeController extends Controller{
    static $catArr=array();
    static $catId='';
    /**  分类题量计算
     * @param int $catId
     * @param int $type
     * @return array
     */
    static public function getExamNum($catId,$type){
        if($type == 1){
            $model=M('Questions');
            ComputeController::$catArr=array();
            $catlist=ComputeController::catWhere($catId);
            if(empty($catlist)){
                $where['cat_id']=$catId;
                $status=1;
            }else{
                $where['cat_id']=array('in',$catlist);
                $status=0;
            }
        }else{
            $model=M('Exam');
            $where['cat_id']=$catId;
            $status=1;
        }

        $questionNum=$model->where($where)->count();
        return array(
            'num'=>$questionNum,
            'status'=>$status,
        );
    }
    /**获取分类下做题数量
     * @param int $catId
     * @param int $type
     * @param int $userId
     * @return mixed
     */
    static public function getRecordNum($catId,$type,$userId){
        $where['user_id']=$userId;
        if($type == 1){
            $model=M('Record_section');
            $wrong=M('Wrong_section');
            ComputeController::$catArr=array();
            $catlist=ComputeController::catWhere($catId);
            if(empty($catlist)){
                $where['cat_id']=$catId;
                $right=$model->field('rights')->where($where)->find();
                $wrong=$wrong->where($where)->count();
            }else{
                $where['cat_id']=array('in',$catlist);
                $rightli=$model->field('sum(rights) as rights')->where($where)->select();
                $right=$rightli['rights'];
                $wrong=$wrong->where($where)->count();
            }
        }else{
            $model=M('Record_exam');
            $wrong=M('Wrong_exam');
            $where['cat_id']=$catId;
            $right=$model->field('rights')->where($where)->find();
	    $right=$right['rights'];
            $wrong=$wrong->where($where)->count();
        }
        $Num['totals']=((int)$right+(int)$wrong);
        return $Num;
    }
    /**获取分类下错题数量
     * @param int $catId
     * @param int $type
     * @param int $userId
     * @return mixed
     */
    static public function getWrongNum($catId,$type,$userId){
        $where['user_id']=$userId;
        if($type == 1){
            $model=M('wrong_section');
            ComputeController::$catArr=array();
            $catlist=ComputeController::catWhere($catId);
            if(empty($catlist)){
                $where['cat_id']=$catId;
                $Num=$model->where($where)->count();
                $status=1;
            }else{
                $where['cat_id']=array('in',$catlist);
                $Numli=$model->where($where)->count();
                $Num=$Numli['wrong'];
                $status=0;
            }
        }else{
            $model=M('wrong_exam');
            $where['cat_id']=$catId;
            $Num=$model->field('wrong')->where($where)->count();
            $status=1;
        }
        return array('num'=>$Num,'status'=>$status);
    }
    /**获取分类下收藏数量
     * @param int $catId
     * @param int $type
     * @param int $userId
     * @return mixed
     */
    static public function getCollectNum($catId,$type,$userId){
        $where['user_id']=$userId;
        if($type == 1){
            $model=M('Collect_section');
            ComputeController::$catArr=array();
            $catlist=ComputeController::catWhere($catId);
            if(empty($catlist)){
                $where['cat_id']=$catId;
                $Num=$model->where($where)->count();
                $status=1;
            }else{
                $where['cat_id']=array('in',$catlist);
                $Num=$model->where($where)->count();
                $status=0;
            }
        }else{
            $model=M('Collect_exam');
            $where['cat_id']=$catId;
            $Num=$model->where($where)->count();
            $status=1;
        }
        return array('num'=>$Num,'status'=>$status);
    }
    /**        分类条件生成
     * @param int $parent_id
     * @return array 分类数组
     */
    static private function catWhere($parent_id=0){
        $category=M('Category');
        $catlist=$category->where('parent_id='.$parent_id)->select();
        foreach ($catlist as $key => $val){
            ComputeController::$catArr[]=$val['id'];
            ComputeController::catWhere($val['id']);
        }
        return ComputeController::$catArr;
    }

    /**
     * @param $cId
     * @param $userId
     * @return int
     */
    static public function freeSetInc($cId,$userId){
		
        $free=M('Free');$quesOrder=M('Questions_order');
        $catId=ComputeController::getCatId($cId);
        $orderli=$quesOrder->where(array('user_id'=>$userId,'cat_id'=>$catId))->find();
        if(!empty($orderli) && $orderli['status'] == 1){
            $request=1;
        }elseif(empty($orderli)){
             $totalli=D('Category')->field('total')->where('id='.$catId)->find();
             $free->add(array('user_id'=>$userId,'cat_id'=>$catId,'total'=>$totalli['total']));
	     $free->where(array('cat_id'=>$catId,'user_id'=>$userId))->setDec('total');
             $freeli=$free->field('total')->where(array('user_id'=>$userId,'cat_id'=>$catId))->find();
             if((int)$freeli['total'] > 0){
                  $request=1;
             }else{
                  $request=0;
             }  

        }elseif($orderli['status'] != 1){
	        $free->where(array('cat_id'=>$catId,'user_id'=>$userId))->setDec('total');
                       $freeli=$free->field('total')->where(array('user_id'=>$userId,'cat_id'=>$catId))->find();
                       if((int)$freeli['total'] > 0){
                               $request=1;
                      }else{
                               $request=0;
                      }  
		}
        return $request;
    }

    /**
     * @param $catId
     * @return mixed
     */
    static public function getCatId($catId){
        $cat=M('Category');
        $catli=$cat->where('id='.$catId)->find();
        if($catli['level'] == 2){
            $cat_id=$catli['id'];
        }else{
			return ComputeController::getCatId($catli['parent_id']);
		}
        return $cat_id;
    }
}